<template>
  <div class="service-status">
    <el-card class="header-card">
      <template #header>
        <div class="card-header">
          <span>服务状态监控</span>
        </div>
      </template>
      <div class="content">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-card class="service-card" :class="{'service-up': apiService.status === 'up', 'service-down': apiService.status === 'down'}">
              <div class="service-header">
                <el-icon class="service-icon"><Connection /></el-icon>
                <h3>API服务</h3>
              </div>
              <div class="service-status">
                <el-tag :type="apiService.status === 'up' ? 'success' : 'danger'">
                  {{ apiService.status === 'up' ? '正常运行' : '服务中断' }}
                </el-tag>
              </div>
              <div class="service-info">
                <p>响应时间: {{ apiService.responseTime }}ms</p>
                <p>可用性: {{ apiService.availability }}%</p>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="8">
            <el-card class="service-card" :class="{'service-up': databaseService.status === 'up', 'service-down': databaseService.status === 'down'}">
              <div class="service-header">
                <el-icon class="service-icon"><DataLine /></el-icon>
                <h3>数据库服务</h3>
              </div>
              <div class="service-status">
                <el-tag :type="databaseService.status === 'up' ? 'success' : 'danger'">
                  {{ databaseService.status === 'up' ? '正常运行' : '服务中断' }}
                </el-tag>
              </div>
              <div class="service-info">
                <p>连接数: {{ databaseService.connections }}</p>
                <p>查询延迟: {{ databaseService.queryDelay }}ms</p>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="8">
            <el-card class="service-card" :class="{'service-up': cacheService.status === 'up', 'service-down': cacheService.status === 'down'}">
              <div class="service-header">
                <el-icon class="service-icon"><Box /></el-icon>
                <h3>缓存服务</h3>
              </div>
              <div class="service-status">
                <el-tag :type="cacheService.status === 'up' ? 'success' : 'danger'">
                  {{ cacheService.status === 'up' ? '正常运行' : '服务中断' }}
                </el-tag>
              </div>
              <div class="service-info">
                <p>命中率: {{ cacheService.hitRate }}%</p>
                <p>内存使用: {{ cacheService.memoryUsage }}MB</p>
              </div>
            </el-card>
          </el-col>
        </el-row>
        
        <div class="alerts-section" style="margin-top: 30px;">
          <h3>最近告警</h3>
          <el-table :data="alerts" style="width: 100%">
            <el-table-column prop="time" label="时间" width="180" />
            <el-table-column prop="service" label="服务" width="150" />
            <el-table-column prop="level" label="级别" width="100">
              <template #default="scope">
                <el-tag :type="scope.row.level === 'critical' ? 'danger' : scope.row.level === 'warning' ? 'warning' : 'info'">
                  {{ scope.row.level === 'critical' ? '严重' : scope.row.level === 'warning' ? '警告' : '信息' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="message" label="消息" />
          </el-table>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Connection, DataLine, Box } from '@element-plus/icons-vue'

// 模拟服务状态数据
const apiService = ref({
  status: 'up',
  responseTime: 45,
  availability: 99.9
})

const databaseService = ref({
  status: 'up',
  connections: 128,
  queryDelay: 12
})

const cacheService = ref({
  status: 'up',
  hitRate: 92.5,
  memoryUsage: 256
})

// 模拟告警数据
const alerts = ref([
  { time: '2023-04-15 14:30:22', service: 'API服务', level: 'warning', message: '响应时间超过阈值' },
  { time: '2023-04-15 10:15:45', service: '数据库服务', level: 'info', message: '连接数达到峰值' },
  { time: '2023-04-14 16:22:18', service: '缓存服务', level: 'critical', message: '内存使用率超过90%' }
])
</script>

<style scoped>
.service-status {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.service-card {
  text-align: center;
  transition: all 0.3s ease;
}

.service-card.service-up {
  border-left: 4px solid #67c23a;
}

.service-card.service-down {
  border-left: 4px solid #f56c6c;
}

.service-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
}

.service-icon {
  font-size: 24px;
  margin-right: 10px;
  color: #409eff;
}

.service-status {
  margin-bottom: 15px;
}

.service-info p {
  margin: 5px 0;
  color: #666;
}

.alerts-section h3 {
  margin-bottom: 15px;
}
</style>